package buttons
{
	import flash.display.*;
	
	public class BackButton extends SimpleButton
	{
		
		public static const SIDE_PERCENT:Number = 0.35;
		
		private var _width:int;
		private var _height:int;
		private var _triangleSideLength:Number;
		private var _centerLineLength:Number;
		
		public function BackButton( width:int, height:int ) {
			
			setWidth( width );
			setHeight( height );
			setTriangleSideLength();
			findCenterLineLength();
			setStates();
						
		}
		
		private function setStates():void {
			
			upState = createUpState();
			overState = upState;
			downState = upState;
			hitTestState = overState;
			
		}
		
		private function createUpState():Sprite {
			
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill( 0x666666, 0.6 );
			sprite.graphics.drawRoundRect( 0, 0, _width, _height, 3, 3 );
			sprite.graphics.endFill();
				
			var triangle:Sprite = drawTriangle();
			triangle.x = ( _width - triangle.width ) / 2;
			triangle.y = ( _height - triangle.height ) / 2;
			sprite.addChild( triangle );
			
			return sprite;			
			
		}
				
		private function setWidth( width:int ):void {
			
			_width = width;
			
		}
		
		private function setHeight( height:int ):void {
			
			_height = height;
			
		}
		
		private function setTriangleSideLength():void {
			
			if( _width > _height ) {
				
				_triangleSideLength = _height * BackButton.SIDE_PERCENT;
				
			} else {
				
				_triangleSideLength = _width * BackButton.SIDE_PERCENT;
				
			} 			
			
		}
		
		private function findCenterLineLength():void {
			
			var rightRadians:Number = 90 * ( Math.PI / 180 );
			var radians:Number = 60 * ( Math.PI / 180 );
			
			_centerLineLength = ( _triangleSideLength * ( Math.sin( rightRadians ) ) ) / radians;
			
			trace( _centerLineLength );
			
		}
		
		private function drawTriangle():Sprite {
			
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill( 0x444444 );
			sprite.graphics.lineStyle( 1, 0x444444 );
			sprite.graphics.moveTo( 0, _triangleSideLength / 2 );
			sprite.graphics.lineTo( _centerLineLength, 0 );
			sprite.graphics.lineTo( _centerLineLength, _triangleSideLength );
			sprite.graphics.lineTo( 0, _triangleSideLength / 2 );
			sprite.graphics.endFill();
			
			return sprite;
			
		}
		
	}
}
